package com.yxx.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxx.sys.common.DataGridView;
import com.yxx.sys.domain.User;
import com.yxx.sys.service.AppUserService;
import com.yxx.sys.service.LoginInfoService;
import com.yxx.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/changePwd")
public class ChangePwdController {
    LoginInfoService loginInfoService;

    @Autowired
    UserService userService;

    @Autowired
    AppUserService appUserService;

    @PostMapping("/true")
    public DataGridView changePwd(String username, String oldPwd,String newPwd) {
//        System.out.println(username);
//        System.out.println(oldPwd);
//        System.out.println(newPwd);
//        System.out.println("111");
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name",username).eq("pwd", SecureUtil.md5(oldPwd)); //绿色是数据库里面的字段，白色是用户传入的字段
        User user = userService.getOne(queryWrapper);
        System.out.println(user);
        if(BeanUtil.isEmpty(user))
            return new DataGridView(-1, "用户名或者密码错误",null);
        //如果密码正确，则把密码写入数据库中
        user.setPwd(SecureUtil.md5(newPwd));   //设置新密码
        userService.updateById(user);    //更新用户信息
        //这个map只是用来返回信息的
        Map<String, String> map = MapUtil.newHashMap();
        map.put("loginName",user.getLoginName());
        return new DataGridView(200, "修改密码成功",map);
    }
}


























